package com.hwua.tqm;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.theme.SubstanceTerracottaTheme;

public abstract class FatherLogin extends JFrame {
    private static final long serialVersionUID = -4655235896173916415L;
    private JPanel contentPane;
    private JTextField usernameTextField;
    private JPasswordField passwordField;
    private JTextField validateTextField;
    private JLabel jLabelText;
    private String randomText;
    JButton submitButton = new JButton("登 录");
    JButton cancelButton = new JButton("退 出");


    public FatherLogin() {
        this.setBounds(0, 0, 400, 300);
        setTitle("系统登录");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));

        JPanel usernamePanel = new JPanel();
        contentPane.add(usernamePanel);

        JLabel usernameLable = new JLabel("\u7528\u6237\u540D\uFF1A");
        usernameLable.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        usernamePanel.add(usernameLable);

        usernameTextField = new JTextField();
        usernameTextField.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        usernamePanel.add(usernameTextField);
        usernameTextField.setColumns(10);

        JPanel passwordPanel = new JPanel();
        contentPane.add(passwordPanel);
        JLabel passwordLabel = new JLabel("\u5BC6   \u7801\uFF1A");
        passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        passwordPanel.add(passwordLabel);
        passwordField = new JPasswordField();
        passwordField.setColumns(10);
        passwordField.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        passwordPanel.add(passwordField);
        JPanel validatePanel = new JPanel();
        contentPane.add(validatePanel);
        JLabel validateLabel = new JLabel("\u9A8C\u8BC1\u7801\uFF1A");
        validateLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        validatePanel.add(validateLabel);
        validateTextField = new JTextField();
        validateTextField.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        validatePanel.add(validateTextField);
        validateTextField.setColumns(5);
        randomText = randomCode(); //获取生成验证码
        jLabelText = new JLabel(randomText);// 随机验证码
        jLabelText.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        validatePanel.add(jLabelText);

        JPanel buttonPanel = new JPanel();
        contentPane.add(buttonPanel);


        //提交事件绑定
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String inputCode = validateTextField.getText();
                String username = usernameTextField.getText();
                String password = passwordField.getText();
                boolean state = loginSubmit(randomText, inputCode, username, password);
                //登录成功退出当前页面
                if (state) {
                    closeCurrentWindows();
                } else {
                    showErrorMsg("登录失败，请重试");
                }
            }
        });
        submitButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        buttonPanel.add(submitButton);


        //退出事件绑定
        cancelButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(-1);
            }
        });
        cancelButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        buttonPanel.add(cancelButton);

        //pack();// 自动调整窗体大小
        this.setLocationRelativeTo(null);// 让窗体居中显示
        this.setVisible(true); //设置界面显示
    }

    //关闭当前窗口
    public void closeCurrentWindows(){
        this.dispose();
    }
    /**
     * 显示错误消息
     *
     * @param msg
     */
    public static void showErrorMsg(String msg) {
        JOptionPane.showMessageDialog(null, msg, "错误提示", JOptionPane.ERROR_MESSAGE);
    }


    /**
     * 生成随机验证码{建议4-6位字母或者数字组合}
     *
     * @return 验证码
     */
    public abstract String randomCode();

    public abstract boolean loginSubmit(String code, String inputCode, String username, String password);


}